package com.hsmpay.common.dao.Impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.hsmpay.common.dao.BaseDao;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

/**
 * sqlmap 基类
 *
 * @param <Entity>
 * @param <Query>
 * @author 颜铃璋
 *         建立时间：2012-11-22
 */
@Repository("baseDao")
public class BaseDaoSupport<Entity extends Serializable, Query extends Object> extends SqlMapClientDaoSupport implements BaseDao<Entity, Query> {
    //----------------------初始化数据 start
    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    public SqlMapClientTemplate getIbatisTemplate() {
        return getSqlMapClientTemplate();
    }

    @PostConstruct
    public void initSqlMapClient() {
        super.setSqlMapClient(sqlMapClient);
    }
    //----------------------初始化数据 end

    /**
     * 添加 实体
     * sqlKey  insertEntity
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public Query insertEntity(Entity entity) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entity));
        Query addId = (Query) getIbatisTemplate().insert(startWith.append("SqlMap.insertEntity").toString(), entity);
        return addId;
    }

    /**
     * 添加 实体
     *
     * @param sqlKey
     * @param entity
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public Query insertEntity(String sqlKey, Entity entity) throws SQLException {
        return (Query) getSqlMapClient().insert(sqlKey, entity);
    }

    //-----------------------------------------------

    /**
     * 修改 实体
     * sqlKey  updateEntity
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    public int updateEntity(Entity entity) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entity));
        int updateId = getIbatisTemplate().update(startWith.append("SqlMap.updateEntity").toString(), entity);
        return updateId;
    }

    /**
     * 修改 实体
     *
     * @param sqlKey
     * @param entity
     * @return
     * @throws SQLException
     */
    public int updateEntity(String sqlKey, Entity entity) throws SQLException {
        return getSqlMapClient().update(sqlKey, entity);
    }

    //----------------------------------------------

    /**
     * 删除 实体
     * sqlKey  deleteEntityById
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    public int deleteEntity(Entity entity) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entity));
        int resDelete = getIbatisTemplate().delete(startWith.append("SqlMap.deleteEntity").toString(), entity);
        return resDelete;
    }

    /**
     * 删除 实体  根据id
     * sqlKey  deleteEntityById
     *
     * @param entityClass
     * @param query
     * @return
     * @throws SQLException
     */
    public int deleteEntity(Class<Entity> entityClass, Query query) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entityClass));
        int resDelete = getIbatisTemplate().delete(startWith.append("SqlMap.deleteEntityById").toString(), query);
        return resDelete;
    }

    /**
     * 删除 实体
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    public int deleteEntity(String sqlKey, Entity entity) throws SQLException {
        return getSqlMapClient().delete(sqlKey, entity);
    }

    //----------------------------------------

    /**
     * 根据实体参数查找单个数据
     * sqlKey  searchEntity
     *
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public Entity searchEntity(Entity entity) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entity));
        return (Entity) getIbatisTemplate().queryForObject(startWith.append("SqlMap.searchEntity").toString(), entity);
    }

    /**
     * 查询 实体
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public Entity searchEntity(String sqlKey, Entity entity) throws SQLException {
        return (Entity) getSqlMapClient().queryForObject(sqlKey, entity);
    }

    /**
     * introduce (根据Id查找)
     * sqlKey  searchEntityById
     *
     * @param query
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public Entity searchEntityById(Class<Entity> entityClass, Query query) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entityClass));
        return (Entity) getIbatisTemplate().queryForObject(startWith.append("SqlMap.searchEntityById").toString(), query);
    }

    //----------------------------------------

    /**
     * 查询 实体列表
     * sqlKey  searchEntityList
     *
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public List<Entity> searchEntityList(Entity entity) throws SQLException {
        StringBuilder startWith = new StringBuilder(getStartWith(entity));
        return getIbatisTemplate().queryForList(startWith.append("SqlMap.searchEntityList").toString(), entity);
    }

    /**
     * 查询 实体列表
     *
     * @param entity
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public List<Entity> searchEntityList(String sqlKey, Entity entity) throws SQLException {
        return (List<Entity>) getSqlMapClient().queryForList(sqlKey, entity);
    }

    //-----------------------------------------

    /**
     * 查询总数
     * sqlKey  getEntityCount
     * @throws SQLException
     */
    public int getEntityCount(Entity entity) throws SQLException {
        int counter = ((Integer) getIbatisTemplate().queryForObject(getStartWith(entity).append("SqlMap.getEntityCount").toString(), entity));
        return counter;
    }

    /**
     * 查询总数
     *
     * @param sqlMap
     * @param query
     * @throws SQLException
     */
    public int getEntityCount(String sqlMap, Query query) throws SQLException {
        int counter = ((Integer) getIbatisTemplate().queryForObject(sqlMap, query));
        return counter;
    }

    /**
     * introduce (获得类名称 Object)
     *
     * @param entity
     * @return
     * @author Administrator
     * @version v1.0
     * @created Aug 27, 2012
     */
    protected static StringBuilder getStartWith(Object entity) {
        String str = entity.getClass().getSimpleName();
        StringBuilder sb = new StringBuilder(str.substring(0, 1).toLowerCase());
        sb.append(str.substring(1));
        return sb;
    }

    /**
     * introduce (获得类名称 Class)
     *
     * @param entity
     * @return
     * @author Administrator
     * @version v1.0
     * @created Aug 27, 2012
     */
    protected static StringBuilder getStartWith(Class<?> entity) {
        String str = entity.getSimpleName();
        StringBuilder sb = new StringBuilder(str.substring(0, 1).toLowerCase());
        sb.append(str.substring(1));
        return sb;
    }
}
